package com.orange.score.android.service;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.orange.score.app.ScoreApplication;
import com.orange.score.model.match.FollowMatchManager;
import com.orange.score.model.match.Match;
import com.orange.score.model.match.MatchManager;
import com.orange.score.network.ScoreNetworkRequest;
import com.orange.score.service.LiveUpdateChangeCallBack;
import com.orange.score.service.MatchService;
import java.util.ArrayList;
import java.util.List;

public class FollowMatchService extends Service
  implements LiveUpdateChangeCallBack
{
  public static final String FOLLOW_MATCH_BROADCAST = "FOLLOW_MATCH_BROADCAST";
  private static final int FOLLOW_MATCH_READY = 1;
  private static final int FOLLOW_MATCH_UPDATE = 2;
  public static final String FOLLOW_MATCH_UPDATE_TYPE = "FOLLOW_MATCH_UPDATE_TYPE";
  private static final String TAG = FollowMatchService.class.getName();
  static FollowMatchManager followMatchManager = new FollowMatchManager();
  Handler updateFollowMatchManagerHandler = new Handler();

  public static FollowMatchManager getFollowMatchManager()
  {
    return followMatchManager;
  }

  private void notifyUpdate(int paramInt)
  {
    Intent localIntent = new Intent("FOLLOW_MATCH_BROADCAST");
    localIntent.putExtra("FOLLOW_MATCH_UPDATE_TYPE", paramInt);
    sendBroadcast(localIntent);
  }

  public void notifyScoreLiveUpdate(List<String[]> paramList)
  {
    int i = 0;
    int j = paramList.size();
    int k = 0;
    if (k >= j)
    {
      if (i != 0)
        notifyUpdate(2);
      return;
    }
    String[] arrayOfString = (String[])paramList.get(k);
    if ((arrayOfString == null) || (arrayOfString.length < 12));
    while (true)
    {
      Match localMatch;
      do
      {
        while (true)
          ++k;
        String str = arrayOfString[0];
        localMatch = followMatchManager.findMatchById(str);
      }
      while (localMatch == null);
      Log.d(TAG, "notifyScoreLiveUpdate for follow list, match " + localMatch.toString() + " has live change");
      localMatch.updateDataByLiveChange(arrayOfString);
      followMatchManager.createOrUpdateMatch(localMatch);
      i = 1;
    }
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  public void onCreate()
  {
    super.onCreate();
    Log.d(TAG, toString() + " <onCreate>");
    followMatchManager.init(getApplicationContext());
    ((ScoreApplication)getApplication()).getRealtimeMatchService().addScoreLiveUpdateObserver(this);
  }

  public void onDestroy()
  {
    ((ScoreApplication)getApplication()).getRealtimeMatchService().removeScoreLiveUpdateObserver(this);
    followMatchManager.close();
    super.onDestroy();
    Log.d(TAG, toString() + " <Destroy>");
  }

  public void onStart(Intent paramIntent, int paramInt)
  {
    super.onStart(paramIntent, paramInt);
    Log.d(TAG, toString() + " <onStart>");
    updateAllFollowMatch();
    notifyUpdate(1);
  }

  public void updateAllFollowMatch()
  {
    ArrayList localArrayList2;
    MatchManager localMatchManager;
    int j;
    ArrayList localArrayList1 = followMatchManager.getFollowMatchList();
    if (localArrayList1 == null);
    while (true)
    {
      return;
      localArrayList2 = new ArrayList();
      localArrayList2.addAll(localArrayList1);
      localMatchManager = ((ScoreApplication)getApplication()).getRealtimeMatchService().getMatchManager();
      int i = localArrayList1.size();
      j = 0;
      if (j < i)
        break;
      UpdateFollowMatchThread localUpdateFollowMatchThread = new UpdateFollowMatchThread(this, localArrayList2);
      this.updateFollowMatchManagerHandler.post(localUpdateFollowMatchThread);
    }
    Match localMatch1 = (Match)localArrayList1.get(j);
    if (localMatch1.isFinish())
    {
      localArrayList2.remove(localMatch1);
      Log.d(TAG, "skip follow match for update, it's done, match id = " + localMatch1.matchId);
    }
    while (true)
    {
      Match localMatch2;
      do
      {
        while (true)
          ++j;
        localMatch2 = localMatchManager.findMatchById(localMatch1.matchId);
      }
      while (localMatch2 == null);
      followMatchManager.updateMatch(localMatch2);
      localArrayList2.remove(localMatch1);
      Log.d(TAG, "skip follow match for update, it's in latest list, match id = " + localMatch1.matchId);
    }
  }

  private class UpdateFollowMatchTask extends AsyncTask<Match, Void, Match>
  {
    protected Match doInBackground()
    {
      Match localMatch1;
      if ((paramArrayOfMatch != null) && (paramArrayOfMatch.length > 0))
      {
        Match localMatch2 = paramArrayOfMatch[0];
        localMatch2.updateMatchDetail(ScoreNetworkRequest.getMatchDetail(localMatch2.matchId));
        localMatch1 = localMatch2;
      }
      while (true)
      {
        return localMatch1;
        localMatch1 = null;
      }
    }

    protected void onPostExecute()
    {
      if (paramMatch == null);
      while (true)
      {
        return;
        FollowMatchService.followMatchManager.updateMatch(paramMatch);
        FollowMatchService.access$0(this.this$0, 2);
      }
    }
  }

  public class UpdateFollowMatchThread
  implements Runnable
  {
    private static final int UPDATE_FOLLOW_MATCH = 1;
    List<Match> matchList;

    public UpdateFollowMatchThread()
    {
      this.matchList = localObject;
    }

    public void run()
    {
      if (this.matchList == null)
        return;
      int i = this.matchList.size();
      int j = 0;
      while (true)
      {
        if (j >= i);
        Match localMatch = (Match)this.matchList.get(j);
        FollowMatchService.UpdateFollowMatchTask localUpdateFollowMatchTask = new FollowMatchService.UpdateFollowMatchTask(this.this$0, null);
        Match[] arrayOfMatch = new Match[1];
        arrayOfMatch[0] = localMatch;
        localUpdateFollowMatchTask.execute(arrayOfMatch);
        ++j;
      }
    }
  }
}